package com.wyp.didishige.activity.location;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.wyp.didishige.activity.BaseActivity;
import com.wyp.didishige.utils.ToastUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.adapter.PoiAdapter;
import com.wyp.didishige.model.User;
import com.wyp.didishige.wiget.xlist.XListView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;

public class PoiSearchActivity extends BaseActivity implements XListView.IXListViewListener{

    public static final String ADDRESS="address";
    private SwipeRefreshLayout hullSwipe = null;
    private XListView poiinfoList =null;
    PoiSearch poiSearch;
    private SearchView searchView =null;

    PoiAdapter poiAdapter;
    List<PoiInfo> poilist=new ArrayList<PoiInfo>();

    String keyword="";
    User user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poi_search);
        actionBar=getSupportActionBar();
        if (actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }

        user = BmobUser.getCurrentUser(this,User.class);
        init();
    }

    @Override
    protected void initParamter() {
        super.initParamter();
        Bundle bundle=getIntent().getExtras();
        if (bundle!=null){
            keyword = bundle.getString(ADDRESS);
        }
        poiAdapter=new PoiAdapter(this,poilist,user);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        searchView = (SearchView) findViewById(R.id.searchView);
        searchView.onActionViewExpanded();
        poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(poiListener);
        hullSwipe = (SwipeRefreshLayout) findViewById(R.id.hullSwipe);
        poiinfoList = (XListView) findViewById(R.id.poiList);
        hullSwipe.setColorSchemeResources(R.color.btn_blue, R.color.vip_male);
    }

    @Override
    protected void initWidgetListener() {
        super.initWidgetListener();
        hullSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                poiSearch(keyword);
            }
        });
        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page = 0;
                poiSearch(searchView.getQuery().toString());
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                keyword = query;
                page = 0;
                poiSearch(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                page = 0;
                keyword = newText;
                poiSearch(newText);
                return true;
            }
        });

        poiinfoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PoiInfo poiInfo = (PoiInfo) poiAdapter.getItem(position);
                setResult(RESULT_OK, new Intent().putExtra(ADDRESS, poiInfo.name));
                finish();
            }
        });

    }

    @Override
    protected void initWidgetState() {
        super.initWidgetState();
        poiinfoList.setPullRefreshEnable(false);

        poiinfoList.setXListViewListener(this);
        poiinfoList.setAdapter(poiAdapter);
        searchView.setQuery(keyword, false);
        poiSearch(keyword);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_user_edit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.action_settings:
                setResult(RESULT_OK, new Intent().putExtra(ADDRESS, searchView.getQuery().toString()));
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){
        public void onGetPoiResult(PoiResult result){
            //获取POI检索结果
            if (result.getAllPoi()!=null){
                if (page==0){
                    poiinfoList.setPullLoadEnable(true);
                    poiAdapter.setList(result.getAllPoi());
                }else{
                    poiAdapter.appendList(result.getAllPoi());
                }
                page++;
            }else{
                ToastUtil.showShort(PoiSearchActivity.this, result.error.toString());
            }
            hullSwipe.setRefreshing(false);

        }
        public void onGetPoiDetailResult(PoiDetailResult result){
            //获取Place详情页检索结果
        }
    };
    int page = 1;
    /**
     * 热点搜索
     */
    private void poiSearch(String keyword){
        try {
            LatLng latLng=new LatLng(user.getLocation().getLatitude(),user.getLocation().getLongitude());
            poiSearch.searchNearby(new PoiNearbySearchOption().location(latLng).keyword(keyword).pageCapacity(20).pageNum(page));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        poiSearch(keyword);
    }


}
